//
//  CCRTBaseVC.m
//  iSeal6100
//
//  Created by He Yuan on 13-1-23.
//  Copyright (c) 2013年 http://www.heyuan110.com. All rights reserved.
//

#import "BaseVC.h"
#import "CustomUIView.h"

@interface BaseVC ()

@end

@implementation BaseVC

@synthesize universalDelegate;

- (void)dealloc
{
    // unregister as observer for theme status
    [self unregisterAsObserver];
}

- (id)init
{
    self = [super init];
    if (self)
    {
        // register as observer for theme status
        [self regitserAsObserver];
    }
    return self;
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self configureViews:NO];
}



-(void)loadView
{
    CGRect  rect = [[UIScreen mainScreen]applicationFrame];
    CustomUIView *view_ = [[CustomUIView alloc]initWithFrame:rect];
    self.view = view_;
}

- (void)configureViews
{
    [self configureViews:YES];
}

- (void)configureViews:(BOOL)animation
{
    //做换肤控制
    
}

- (void)regitserAsObserver
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self
               selector:@selector(configureViews)
                   name:ThemeDidChangeNotification
                 object:nil];
}

- (void)unregisterAsObserver
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center removeObserver:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIDeviceOrientation)orientation
{
    if(orientation == UIDeviceOrientationPortrait)
        return YES;
    return NO;
}

@end

@implementation UIViewController(BaseViewController)

-(void)loadViewController:(NSString *)viewController
{
    [self loadViewController:viewController hidesBottomBarWhenPushed:NO];
}

-(void)loadViewController:(NSString *)viewController hidesBottomBarWhenPushed:(BOOL)ishide
{
    UIViewController *vc = [[NSClassFromString(viewController) alloc]init];
    [vc setHidesBottomBarWhenPushed:ishide];
    if (ishide)[(TabBarViewController *)self.tabBarController setCustomTabBarHidden:YES];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)setTitle:(NSString *)title showBackButton:(BOOL)showBackButton
{
    if (title && [title isKindOfClass:[NSString class]] && [title length]>0)
    {
        UILabel *titleLbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 44)];
        [titleLbl setTextAlignment:UITextAlignmentCenter];
        titleLbl.textColor = ThemeTitleColor;
        titleLbl.backgroundColor = [UIColor clearColor];
        titleLbl.font = FONT_TITLE;
        titleLbl.text = title;
        self.navigationItem.titleView = titleLbl;
    }
    
    if (showBackButton)
    {
        [self setBackItem];
    }
    else
    {
        self.navigationItem.leftBarButtonItem = nil;
        self.navigationItem.hidesBackButton = YES;
        self.navigationItem.backBarButtonItem = nil;
    }
}


-(void)setBackItem
{
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.backBarButtonItem = nil;
    UIImage *btnImage = [UIImage imageNamed:@"nav_btn_back"];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(0, 0, btnImage.size.width, btnImage.size.height)];
    [btn setBackgroundImage:[btnImage stretchableImageWithLeftCapWidth:2 topCapHeight:2] forState:UIControlStateNormal];
    btn.tag = 1;
    [btn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = leftItem;
}

-(void)setLeftItem:(NSString *)title
{
    UIImage *btnImage = ThemeImage(@"nav_btn_rightitem");
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setFrame:CGRectMake(0, 0, btnImage.size.width, btnImage.size.height)];
    [leftBtn setBackgroundImage:[btnImage stretchableImageWithLeftCapWidth:2 topCapHeight:2] forState:UIControlStateNormal];
    [leftBtn setTitle:title forState:UIControlStateNormal];
    [leftBtn setTitleColor:ThemeBackButtonTitleColor forState:UIControlStateNormal];
    [leftBtn.titleLabel setFont:FONT_NORMAL];
    leftBtn.tag = 2;
    [leftBtn addTarget:self action:@selector(leftBarButtonItemAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
}

-(void)leftBarButtonItemAction:(UIButton *)btn
{
    //reload in subclass
}

-(void)setRightItem:(NSString *)title
{
    UIImage *btnImage = ThemeImage(@"nav_btn_rightitem");
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setFrame:CGRectMake(0, 0, btnImage.size.width, btnImage.size.height)];
    [rightBtn setBackgroundImage:[btnImage stretchableImageWithLeftCapWidth:2 topCapHeight:2] forState:UIControlStateNormal];
    [rightBtn setTitle:title forState:UIControlStateNormal];
    [rightBtn setTitleColor:ThemeBackButtonTitleColor forState:UIControlStateNormal];
    [rightBtn.titleLabel setFont:FONT_NORMAL_BOLD];
    rightBtn.tag = 3;
    [rightBtn addTarget:self action:@selector(rightBarButtonItemAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}


-(void)setRightImageItem:(NSString *)image
{
    UIImage *btnImage = [UIImage imageNamed:image];
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setFrame:CGRectMake(0, 0, btnImage.size.width, btnImage.size.height)];
    [rightBtn setBackgroundImage:btnImage forState:UIControlStateNormal];
    rightBtn.tag = 3;
    [rightBtn addTarget:self action:@selector(rightBarButtonItemAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}

-(void)rightBarButtonItemAction:(UIButton *)btn
{
    //reload in subclass
}

-(void)back:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end


